package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.annotations.CurrentUser;
import io.geekidea.springbootplus.system.entity.*;
import io.geekidea.springbootplus.system.param.CarrierSchoolPageParam;
import io.geekidea.springbootplus.system.service.CarrierSchoolService;
import io.geekidea.springbootplus.system.service.CarrierService;
import io.geekidea.springbootplus.system.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-10
 */
@Slf4j
@RestController
@RequestMapping("/carrierSchool")
@Module("system")
@Api(value = "API", tags = {""})
public class CarrierSchoolControllerExtra extends BaseController {

    @Autowired
    private CarrierSchoolService carrierSchoolService;

    @Autowired
    private CarrierService carrierService;

    @Autowired
    private SchoolService schoolService;

    /**
     * 分页列表
     */
    @PostMapping("/getBySchool")
    @OperationLog(name = "所有列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "所有列表", response = CarrierSchool.class)
    public ApiResult<List<Carrier>> getAll(@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        int schoolID = currentAdminInfo.getSchool().getSchoolID();
        LambdaQueryWrapper<CarrierSchool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CarrierSchool::getSchoolID,schoolID).eq(CarrierSchool::getDeleted,0);
        List<CarrierSchool> list = carrierSchoolService.getAllList(wrapper);
        List<Carrier> l = new ArrayList<>();
        for (CarrierSchool carrierSchool : list) {
            Carrier carrier = carrierService.getById(carrierSchool.getCarrierID());
            if(carrier!=null){
                l.add(carrier);
            }
        }
        return ApiResult.ok(l);
    }

    /**
     * 添加
     */
    @PostMapping("/close")
    @OperationLog(name = "关闭", type = OperationLogType.ADD)
    @ApiOperation(value = "关闭", response = ApiResult.class)
    public ApiResult<Boolean> close(@RequestBody JSONObject jsonObject) throws Exception {

        int schoolID = jsonObject.getInteger("schoolID");
        int carrierID = jsonObject.getInteger("carrierID");

        CarrierSchool carrierSchool = carrierSchoolService.getOne(new LambdaQueryWrapper<CarrierSchool>().eq(CarrierSchool::getSchoolID, schoolID).eq(CarrierSchool::getCarrierID,carrierID));

        carrierSchool.setDeleted(1);

        carrierSchoolService.saveOrUpdate(carrierSchool);

        return ApiResult.result(true);
    }

    /**
     * 添加
     */
    @PostMapping("/pass")
    @OperationLog(name = "审批", type = OperationLogType.ADD)
    @ApiOperation(value = "审批", response = ApiResult.class)
    public ApiResult<Boolean> apply(@RequestBody JSONObject jsonObject) throws Exception {

        int schoolID = jsonObject.getInteger("schoolID");
        int carrierID = jsonObject.getInteger("carrierID");

        CarrierSchool carrierSchool = carrierSchoolService.getOne(new LambdaQueryWrapper<CarrierSchool>().eq(CarrierSchool::getSchoolID, schoolID).eq(CarrierSchool::getCarrierID,carrierID));

        carrierSchool.setDeleted(0);

        carrierSchoolService.saveOrUpdate(carrierSchool);

        Carrier carrier = carrierService.getById(carrierSchool.getCarrierID());

        carrier.setDeleted(0);

        carrierService.saveOrUpdate(carrier);

        return ApiResult.result(true);
    }

    @PostMapping("/refuse")
    @OperationLog(name = "拒绝", type = OperationLogType.ADD)
    @ApiOperation(value = "拒绝", response = ApiResult.class)
    public ApiResult<Boolean> refuse(@RequestBody JSONObject jsonObject) throws Exception{
        String schoolID = jsonObject.getString("schoolID");
        String carrierID = jsonObject.getString("carrierID");

        CarrierSchool carrierSchool = carrierSchoolService.getOne(new LambdaQueryWrapper<CarrierSchool>().eq(CarrierSchool::getSchoolID, schoolID).eq(CarrierSchool::getCarrierID,carrierID));

        carrierSchoolService.removeById(carrierSchool.getId());
        return ApiResult.result(true);
    }
    /**
     * 等待审批
     */
    @PostMapping("/waitingCheck")
    @OperationLog(name = "等待审批", type = OperationLogType.ADD)
    @ApiOperation(value = "等待审批", response = ApiResult.class)
    public ApiResult<List<Carrier>> waitingCheck(@RequestBody JSONObject jsonObject) throws Exception {

        LambdaQueryWrapper<CarrierSchool> wrapper = new LambdaQueryWrapper<CarrierSchool>()
                .eq(CarrierSchool::getDeleted,1);

        String schoolID = jsonObject.getString("schoolID");

        if(!StringUtils.isEmpty(schoolID)){
            wrapper.eq(CarrierSchool::getSchoolID,schoolID);
        }

        List<CarrierSchool> list = carrierSchoolService.list(wrapper);

        ArrayList<Carrier> l = new ArrayList<>();

        list.forEach(item->{
            Carrier carrier = carrierService.getById(item.getCarrierID());
            School school = schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getSchoolID, item.getSchoolID()));
            carrier.setSchoolName(school.getSchoolName());
            carrier.setSchoolID(school.getSchoolID());
            l.add(carrier);
        });
        return ApiResult.ok(l);
    }

    @PostMapping("/manage")
    @OperationLog(name = "等待审批", type = OperationLogType.ADD)
    @ApiOperation(value = "等待审批", response = ApiResult.class)
    public ApiResult<List<Carrier>> all(@RequestBody JSONObject jsonObject) throws Exception {

        LambdaQueryWrapper<CarrierSchool> wrapper = new LambdaQueryWrapper<CarrierSchool>();

        String schoolID = jsonObject.getString("schoolID");

        if(!StringUtils.isEmpty(schoolID)){
            wrapper.eq(CarrierSchool::getSchoolID,schoolID);
        }

        List<CarrierSchool> list = carrierSchoolService.list(wrapper);

        ArrayList<Carrier> l = new ArrayList<>();

        list.forEach(item->{
            Carrier carrier = carrierService.getById(item.getCarrierID());
            School school = schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getSchoolID, item.getSchoolID()));
            carrier.setSchoolName(school.getSchoolName());
            carrier.setSchoolID(school.getSchoolID());
            carrier.setDeleted(item.getDeleted());
            l.add(carrier);
        });
        return ApiResult.ok(l);
    }
}


